<template>
    <div class="searchj-box">
      <div class="row">
        <div class="left">
          <el-icon>
            <Search />
          </el-icon>
          <span>筛选搜索</span>
        </div>
        <div class="right">
          <el-button @click="reset()">重置</el-button>
          <el-button type="primary" @click="queryUserList">查询搜索</el-button>
        </div>
      </div>
      <div class="row">
        <span>活动名称:</span>
        <el-input placeholder="活动名称" v-model="queryParams.keyword"></el-input>
      </div>
    </div>
    <div class="add-area">
      <div class="text"><el-icon><Tickets /></el-icon><span>数据列表</span></div>
      <div>    
        <el-button @click="router.push(`/sms/flashSession`)">秒杀时间段列表</el-button>
        <el-button @click="showDialog = true">添加活动</el-button>
    </div>
    </div>
    <el-table
    ref="multipleTableRef"
    :data="userList"
    style="width: 100%"
    @selection-change=""
    border 
    cell-class-name="align-c"
  >
    <el-table-column type="selection" width="55" />
    <el-table-column label="编号" prop="id" />
    <el-table-column label="活动标题" prop="title" />
    <el-table-column label="活动状态"  >活动未开始</el-table-column>
    <el-table-column label="开始时间" prop="createTime">
      <template #default="{ row }">
        {{ formateDate(row.startDate) }}
      </template>
    </el-table-column>
    <el-table-column label="结束时间" prop="loginTime">
      <template #default="{ row }">
        {{ formateDate(row.endDate) }}
      </template>
    </el-table-column>
    <el-table-column label="上线/下线" prop="loginTime">
      <template #default="{ row }">
        <el-switch v-model="row.status" :active-value="1" :inactive-value="0" @change="switchStatue(row)" />
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button text type="primary" @click="router.push('/sms/selectSession?flashPromotionId='+row.id)"  size="small">设置商品</el-button>
          <el-button text type="primary" @click="change(row.id)"  size="small">编辑</el-button>
          <el-button text type="primary" @click="del(row.id)"  size="small">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  
    <ElConfigProvider :locale="zhCn">
      <div class="page">
        <el-pagination v-model:current-page="queryParams.pageNum" v-model:page-size="queryParams.pageSize"
          :page-sizes="[5, 10, 20, 50]" background layout="total, sizes, prev, pager, next, jumper" :total="total"
          @size-change="queryUserList" @current-change="queryUserList" />
      </div>
    </ElConfigProvider>
    <editCoupon :show="showDialog" @hide="closeDialog" :id="editId" />

  </template>
  <script setup lang="ts">
  import { ref, reactive } from "vue";
  import { getFlashList,delActivity ,SwitchActivity} from "./api";
  import { Search,Tickets } from "@element-plus/icons-vue"
  import { ElConfigProvider, ElMessage, ElMessageBox } from 'element-plus'
  import zhCn from 'element-plus/lib/locale/lang/zh-cn'
  import { formateDate } from '@/utils'
  import editCoupon from './editCoupon.vue'
import { useRouter } from "vue-router";
  const router=useRouter()
  const editId = ref(0);
  const change = (id: number) => {
    editId.value = id;
    showDialog.value = true;
  }

  const switchStatue=(row:TimeListModel)=>{
  ElMessageBox.confirm("是否要修改该状态??", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        SwitchActivity(row.id!,row.status).then((res)=>{
      if(res.code===200){
        ElMessage.success("修改成功")
      }else{
        row.status= row.status===1?0:1
      }
    }).catch(()=>{
      row.status= row.status===1?0:1
    })
      })
      .catch(() => {
        ElMessage('取消操作')
        row.status= row.status===1?0:1
      });
  }

  const del=(id:number)=>{
    ElMessageBox.confirm("是否要删除该活动?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        delActivity(id).then((res) => {
          if (res.code === 200) {
            ElMessage.success("删除成功");
            queryUserList()
          }
        });
      })
      .catch(() => {});
  }

  const showDialog = ref(false);
  const closeDialog = (isReload?: boolean) => {
    showDialog.value = false;
    if (isReload) queryUserList();
  };

  const reset = () => {
      if (queryParams.keyword?.trim()) {
          queryParams.keyword = ''
          queryUserList()
      }
  }
  const queryParams = reactive<IUserListParams>({
    keyword: '',
    pageNum: 1,
    pageSize: 5
  })
  
  const userList = ref<IUserModel[]>([]);
  const total = ref(0)
  
  const queryUserList = () => {
    getFlashList(queryParams).then((res) => {
      if (res.code === 200) {
        // console.log(res.data);
        
        userList.value = res.data.list;
        total.value = res.data.total
      }
    });
  };
  queryUserList()
  </script>
  <style lang="less" scoped>
  .searchj-box {
    box-shadow: 0 0 4px #ccc;
    border-radius: 10px;
    padding: 20px;
    margin-bottom: 20px;
  
    .row {
      padding: 20px 0;
      display: flex;
  
      &:first-child {
        justify-content: space-between;
      }
  
      &:last-child {
        width: 400px;
  
        span {
          width: 100px;
        }
      }
    }
  }
  
  .page {
    padding: 20px;
    display: flex;
    justify-content: flex-end;
  }
  
  :deep(.align-c) {
    .cell {
      text-align: center !important;
    }
  }
  .add-area {
    display: flex;
    justify-content: space-between;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 0 4px #ccc;
    border-radius: 10px;
    .text{
      display: flex;
      align-items: center;
      span{
        padding-left: 10px;
      }
    }
  }
  </style>
  